package com.beiing.tianshuai.tianshuai.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by magikliu on 2017/7/7.
 */

public class DateUtil {

    //当前时间
    public static Date DATE_NOW = new Date();
    private String formatTime;

    //字符串转时间戳
    public static String getCurrentTime(String timeString) {
        String timeStamp = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        Date d;
        try {
            d = sdf.parse(timeString);
            long l = d.getTime();
            timeStamp = String.valueOf(l);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return timeStamp;
    }

    //时间戳转字符串
    public static String getStrTime(String timeStamp) {
        String timeString;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd hh:mm");
        long l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l * 1000));
        return timeString;
    }

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds
     * @param format
     * @return
     */
    public static String timeStamp2Date(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || seconds.equals("null")) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    /**
     * 日期格式字符串转换为时间戳
     *
     * @param date_str
     * @param format
     * @return
     */
    public static String date2TimeStamp(String date_str, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return String.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return "";
    }

    //获取上次更新时间
    public static String getLastUpdateTime(String timeStamp) {
        String timeString;
        SimpleDateFormat sdf = new SimpleDateFormat("上次更新 M-d HH:mm");
        long l = Long.valueOf(timeStamp);
        timeString = sdf.format(new Date(l * 1000));
        return timeString;
    }

    /**
     * 返回多少时间之前
     *
     * @param timeStamp
     * @return
     */
    public static String getTime(String timeStamp) {
        long publishTime = Long.parseLong(timeStamp);
        long currentTime = System.currentTimeMillis() / 1000;
        long timeDifference = currentTime - publishTime;
        long timeDifferenceHour = timeDifference / 3600;
        if (timeDifference / 60 < 1) {
            return "刚刚";
        } else if (timeDifferenceHour < 1) {
            return (timeDifference / 60) + "分钟前";
        } else if (timeDifferenceHour < 24) {
            return timeDifferenceHour + "小时前";
        } else if (timeDifferenceHour / 24 < 30) {
            return (timeDifferenceHour / 24) + "天前";
        } else {
            return DateUtil.getStrTime(timeStamp);
        }
    }

    //返回指定格式的时间
    public static String getFormatTime(Date time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String formatTime = dateFormat.format(time);
        return formatTime;
    }

    //返回首页活动列表时间
    public static String getActiveTime(String beginTimeStamp, String endTimeStamp) {
        long beginTime = Long.parseLong(beginTimeStamp);
        long endTime = Long.parseLong(endTimeStamp);
        long currentTime = System.currentTimeMillis() / 1000;
        long timeDifference;
        if (beginTime - currentTime > 0) {
            timeDifference = (beginTime - currentTime) / 3600;
            if (timeDifference < 24) {
                return "即将开始";
            } else {
                return (timeDifference / 24) + "天后";
            }
        } else if (endTime - currentTime > 0) {
            return "报名中";
        } else {
            return "已结束";
        }
    }
}
